home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
answrbok
/
6_3.lha
/
6_3
/
6_3d.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-08-08
|
1KB
|
63 lines
* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
* The C++ Answer Book */
* Tony Hansen */
* All rights reserved. */
/ assign a string to a substring, version 2
/ handling mismatches in lengths
include <memory.h>
include <string.h>
/ assign a character string to a substring
ubstring& substring::operator=(char *cp)
// disconnect string
if (str->p->n > 1)
{
str->p->n--;
struct srep *oldp = str->p;
str->p = new struct srep;
str->p->s = new char[strlen(oldp->s) + 1];
strcpy(str->p->s, oldp->s);
str->p->n = 1;
s = str->p->s + (s - oldp->s);
}
int stlen = strlen(cp);
// same size, simply copy data
if (len == stlen)
{
if (len > 0)
memcpy(s, cp, len);
}
// expand or shrink data area
else
{
// allocate the new data segment
char *sps = str->p->s;
const int s1len = s - sps;
const int s3len = strlen(sps) - len - s1len;
char *newdata =
new char[s1len + stlen + s3len + 1];
// copy in the data
if (s1len > 0)
memcpy(newdata, sps, s1len);
if (stlen > 0)
memcpy(newdata + s1len, cp, stlen);
if (s3len > 0)
memcpy(newdata + s1len + stlen,
s + len, s3len);
newdata[s1len + stlen + s3len]= '\0';
// replace pointers
delete str->p->s;
str->p->s = newdata;
s = newdata + s1len;
len = stlen;
}
return *this;